package thread.synchroCode;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.Test2")
public class _07WaitNotify3 {

  // flag 表示 a是否被执行过
  private static Boolean flag = false;
  private static Object lock= new Object();

  public static void main(String[] args) {

    Thread t1 = new Thread(() -> {
      synchronized (lock) {
        log.debug("a");
        flag = true;
        lock.notifyAll();
      }
    });

    Thread t2 = new Thread(() -> {
      synchronized (lock) {
        while (!flag) {
          try {
            lock.wait();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        log.debug("b");
      }
    });

    t2.start();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    t1.start();
  }

}
